package cn.zzuisa.bluebox.kit;

import cn.zzuisa.bluebox.base.strutils.JacksonJsonUtil;
import cn.zzuisa.bluebox.utils.wxres.LoginCodeRequestRetu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NetIOUtils {

	public static String get(String ul) {
		InputStream is = null;
		InputStreamReader isr = null;
		try {
			StringBuilder sb = new StringBuilder();
			URL url = new URL(ul);
			HttpURLConnection ocn = (HttpURLConnection) url.openConnection();
			ocn.setReadTimeout(10000);
			ocn.setConnectTimeout(20000);
			is = ocn.getInputStream();
			isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String msg = null;
			while((msg = br.readLine()) != null) {
				sb.append(msg);
			}
			return sb.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(isr != null) {
						isr.close();
				}
				if(is != null) {
					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	public static void main(String[] args) {
		String url = "https://api.weixin.qq.com/sns/jscode2session?appid=wxe151d5e1731b33e7&secret=7a02389b92ccc821030471921cf9b5a8&js_code=021k6cQX1sSKG01vJBMX1SAZPX1k6cQo&grant_type=authorization_code";
		url = get(url);
		LoginCodeRequestRetu object = JacksonJsonUtil.toObject(url, LoginCodeRequestRetu.class);
		System.out.println(object);
	}

}
